﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for HDSudungController
/// </summary>
public class HDSudungController:Interface
{
    private SqlConnection con;
	public HDSudungController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_HDSD";
            cmd.CommandType = CommandType.StoredProcedure;
            HDSudung hd = (HDSudung)obj;
            cmd.Parameters.Add("@title", SqlDbType.NText).Value = hd.Title;
            cmd.Parameters.Add("@content", SqlDbType.NText).Value = hd.Content;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = hd.Url;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = hd.Status;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = hd.DateStart;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_HDSD";
            cmd.CommandType = CommandType.StoredProcedure;
            HDSudung hd = (HDSudung)obj;
            cmd.Parameters.Add("@HD_id", SqlDbType.Int).Value = hd.HD_Sudung_id;
            cmd.Parameters.Add("@title", SqlDbType.NText).Value = hd.Title;
            cmd.Parameters.Add("@content", SqlDbType.NText).Value = hd.Content;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = hd.Url;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = hd.Status;
            cmd.Parameters.Add("@dateUpdate", SqlDbType.DateTime).Value = hd.DateUpdate;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_HDSD";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@HD_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_HDSD WHERE [status]=1 ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_HDSD WHERE HD_id=@HD_id AND [status]=1";
            cmd.Parameters.AddWithValue("@HD_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetByProductID(int product_id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Product_HD_Sudung WHERE product_id=@product_id";
            cmd.Parameters.AddWithValue("@product_id", product_id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
}
